from flask import jsonify
from API import OperationMysql, request, app
import time
import json
# 自定义的数据库类
op_mysql = OperationMysql()

# 查询所有未完成任务
# post
@app.route('/sentence/index', methods=['post'])
def get_sentence():
    if request.method == 'POST':
        res = op_mysql.search_one("SELECT * FROM sentence WHERE state = '1' order by id desc")
        return jsonify(res)

# 添加任务
# post
@app.route('/sentence/add', methods=['post'])
def add_sentence():
    if request.method == 'POST':
        formData = 'multipart/form-data'
        isFormData = formData in request.headers['Content-Type']
        jsonObj = {
            'content': '',
            'author': '',
            'type': ''
        }
        if isFormData:
            jsonObj.content = request.form['content']
            jsonObj.author = request.form['author']
            jsonObj.type = request.form['type']
        else:
            jsonObj = json.loads(request.data.decode("utf-8"))
        t = time.time()
        t = int(t)
        res = op_mysql.insert_one("INSERT INTO sentence (content, creation_time, state, id, update_time, author, type) VALUES ('%s','%s',1,0,'%s','%s','%s')" % (jsonObj['centent'], t, t, jsonObj['author'], jsonObj['type']))
        return jsonify(res)


def Sentence():
    print("\033[1;32m执行句子APi\033[0m")